package cn.swust.designpatternlearningplatform.service.impl;

import cn.swust.designpatternlearningplatform.dao.SelfTrainingDao;
import cn.swust.designpatternlearningplatform.dao.TopicDao;
import cn.swust.designpatternlearningplatform.dao.TopicOptionDao;
import cn.swust.designpatternlearningplatform.domain.bo.NodeBo;
import cn.swust.designpatternlearningplatform.domain.bo.OptionsBo;
import cn.swust.designpatternlearningplatform.domain.bo.TrainingTopicBo;
import cn.swust.designpatternlearningplatform.domain.dto.TreeDto;
import cn.swust.designpatternlearningplatform.domain.entity.KnowledgePoint;
import cn.swust.designpatternlearningplatform.domain.entity.Topic;
import cn.swust.designpatternlearningplatform.service.SelfTrainingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
@Transactional
public class SelfTrainingServiceImpl implements SelfTrainingService {
    @Resource
    private SelfTrainingDao selfTrainingDao;
    @Resource
    private TopicOptionDao topicOptionDao;
    @Resource
    private TopicDao topicDao;

    /**
     * @return List<AllPointDto>
     * @author wang
     * description 返回多级目录
     * date 2023/4/18 20:10
     */
    @Override
    public TreeDto getAllPoint() {

        List<KnowledgePoint> list = selfTrainingDao.getAllPoint();
        NodeBo nodeBo = new NodeBo(0, "根节点");
        TreeDto treeDto = new TreeDto(nodeBo);
        for (int i = 0; i < list.size(); i++) {
            KnowledgePoint knowledgePoint = list.get(i);
            if (knowledgePoint.getParentId() == null) {
                NodeBo nodeBo1 = new NodeBo(knowledgePoint.getPointId(), knowledgePoint.getPointContent());
                treeDto.getRoot().addChild(nodeBo1);
            } else {
                NodeBo nodeBo1 = treeDto.getNodeById(knowledgePoint.getParentId());
                NodeBo nodeBo2 = new NodeBo(knowledgePoint.getPointId(), knowledgePoint.getPointContent());
                nodeBo1.addChild(nodeBo2);
            }
        }
        return treeDto;
    }

    @Override
    public List<TrainingTopicBo> getTopicByPointId(Integer pointId) {
        //查询题目id
        List<Integer> topicIds = selfTrainingDao.getTopicByPointId(pointId);
        if (topicIds.isEmpty()) {
            return new ArrayList<>();
        }
        //默认10道，下标为9
        int numIndex = Math.min(5, topicIds.size() - 1);
        //随机抽取10道题目
        Collections.shuffle(topicIds);
        List<Integer> subTopicIds = topicIds.subList(0, Math.min(numIndex, topicIds.size()));
        numIndex++;
        List<TrainingTopicBo> trainingTopicBoList = new ArrayList<>();
        for (int i = 0; i < subTopicIds.size(); i++) {
            TrainingTopicBo topicBo = new TrainingTopicBo();
            Topic topic = topicDao.selectTopicById(subTopicIds.get(i));
            if (topic == null) {
                if (numIndex < topicIds.size()) {
                    subTopicIds.add(topicIds.get(numIndex++));
                }
                continue;
            }
            List<OptionsBo> options = topicOptionDao.getTopicOptions(topic.getTopicId());
            topicBo.setOptions(options);
            topicBo.setTopicId(topic.getTopicId());
            topicBo.setTopicType(topic.getTopicType());
            topicBo.setTopicContent(topic.getTopicContent());
            topicBo.setTopicContentPath(topic.getTopicContentPath());
            topicBo.setTopicDifficulty(topic.getTopicDifficulty());
            trainingTopicBoList.add(topicBo);
        }
        return trainingTopicBoList;
    }
}
